<?php

abstract class Abstracts{
	
	protected $_db;
	
	protected $_table;
	
	protected function query($sql){
		return 	$this->_db->query($sql);	
	}
	
	public function __construct(){
		$this->_db = Db::getInstance();		
	}
	public function getTable(){
		return $this->_table;
	}
	
	public function getId(){
		$re = $this->_db->query("insert into auto_id Values()");
		return $this->_db->lastInsertId();
	}
	/**
	 * @todo 插入一条记录
	 * @param unknown_type $data =>array('col'=>value);
	 * @return boolean
	 */
	public function insert($data){
		if(is_array($data)){
			$fields = $this->_db->list_fields($this->_table); //			
			$sql    = "insert into {$this->_table} set ";
			$setArr = array(); 
			foreach($data as $k=>$v){
				if(in_array($k,$fields)){
					$setArr[] = "$k = '{$v}'";
				}
			}			
			if(!empty($setArr)){
				$sql    .= implode(",", $setArr);				
				$affect = $this->_db->exec($sql);
				if($affect)
					return $this->_db->lastInsertId();
				else
					return false;
			}
		}
		return false;
	}
	/**
	 * @todo 查询一条记录
	 * @param unknown_type $sql
	 * @return multitype:
	 */
	public function getRow($sql){
		$query = $this->_db->query($sql);
		if($query){
			return $query->fetch();
		}
		return false;		
	}
	/**
	 * 查询所有记录
	 * @param unknown_type $sql
	 */
	public function getAll($sql){
		$query = $this->_db->query($sql);
		return $query->fetchAll();
	}
	/**
	 * 根据
	 * @param unknown_type $where 数组array('field'=>'值')：字符串-where字符串：null
	 * @param unknown_type $order
	 * @param unknown_type $fields
	 * @return Ambigous <multitype:, boolean>
	 */
	public function getRowByWhere($where = null,$order=null,$fields=null){
		$sql = $this->_pinkSql($where,$order,$fields,"1");
		return $this->getRow($sql);
	}
	/**
	 * 
	 * @param unknown_type $where
	 * @param unknown_type $order
	 * @param unknown_type $fields
	 * @param unknown_type $limit
	 */
	public function getAllByWhere($where = null,$order=null,$fields=null,$limit=null){
		$sql = $this->_pinkSql($where,$order,$fields,$limit);
		return $this->getAll($sql);
	}
	/**
	 * 给定where条件 判断数据是否存在
	 * @param unknown_type $where
	 * @return boolean
	 */
	public function isExist($where = null){		
		$rs  = $this->getRowByWhere($where);
		return $rs?true:false;
	}
	/**
	 * 拼装一个sql语句
	 * @param unknown_type $where
	 * @param unknown_type $order
	 * @param unknown_type $fields
	 * @param unknown_type $limit
	 */
	protected function _pinkSql($where=null,$order=null,$fields=null,$limit=null){
		if($where && is_array($where)){						
			$wArr = array();
			foreach($where as $wkey=>$wval){
				$wArr[] = $wkey . '=' . "'$wval'";
			}	
			$where = implode(" and ", $wArr);						
		}
		$where = $where?" where " . $where . ' ':'';
		$fields = !$fields?"*":$fields;
		$fields = is_array($fields)?implode(',', $fields):$fields;
		$order  = $order?" order by " . $order . " ":"";
		$limit  = $limit?" limit " . $limit . " " : "";
		$sql = "select {$fields} from {$this->_table} {$where} {$order} {$limit}";
		return $sql;
		
	}
	
	public function update($data,$where=null){
		if($where && is_array($where)){
			$wArr = array();
			foreach($where as $wkey=>$wval){
				$wArr[] = $wkey . '=' . "'$wval'";
			}
			$where = implode(" and ", $wArr);
			$where = $where?" where " . $where . ' ':'';
		}elseif(!$where && isset($data['id'])){			
			$where = " where id = '{$data['id']}'";
			unset($data['id']);					
		}
		if(!is_string($where) || empty($data))
			return false;		
		$fields = $this->_db->list_fields($this->_table);
		$sql    = "update {$this->_table} set ";
		$setArr = array();
		foreach($data as $k=>$v){
			if(in_array($k,$fields)){
				$setArr[] = "$k = '{$v}'";
			}
		}
		if(!empty($setArr)){
			$sql .= implode(",", $setArr) . $where;	
			return $this->_db->query($sql);
		}
		
		return false;
	}
	/**
	 * 删除数据
	 * @param unknown_type $cond 为数据记录ID或者where语句
	 * @return boolean
	 */
	public function delete($cond){
		if(!$cond)
			return false;
		if(is_numeric($cond))
			$sql = "delete from {$this->_table} where id = '$cond'";
		else
			$sql = "delete from {$this->_table} where $cond";
		return $this->_db->query($sql);
	}
	public function getCount($where){		
		$sql = "select count(id) as c from {$this->_table}";
		$sql .= $where? " where ".$where:""; 
		$re   = $this->getRow($sql);
		return isset($re['c'])?$re['c']:0;
	}
}